#coding=utf-8
import re
import urllib2

## 设定 urllib2 的 User-Agent
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent','Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)')]

def _html2txt(s):
    '''去掉 html 标记
    '''
    return re.sub(r'<[^>]+>', '', s)
        
def _gethtml_from_com(key, num=25, page=0):
        
    key = urllib2.quote(key)
    start = num*int(page)
    url = 'http://www.google.com/search?q=%s&num=%s&start=%s'%(key, num, start)
    
   # print url
        
    try:
        return opener.open(url).read()
    except Exception,e :
        print e
        print key, 'error'
        return ''
        
def _gethtml_from_cn(key, num=25, page=0):
    key = urllib2.quote(key)
    start = num*int(page)
    url = 'http://www.google.cn/search?q=%s&num=%s&start=%s&hl=zh-CN'%(key, num, start)
    
    #print url
        
    try:
        return opener.open(url).read()
    except Exception,e :
        print e
        print key, 'error'
        return ''

def _dorev(s):
    '''分析抓回的 google 搜索结果页面 
    return [[url, title, summary],]
    '''
    if not s: return []
    rev=[]
    s1 = s.split(r'<h3 class=r>')[1:]
    for i in s1:
            if i.rfind('<span class="f">文件格式:</span>')>-1:
                    continue
            #url = i.split('"')[1]
            try:
                url = re.findall('href=".*?"', i)[0][6:-1]
            except:
                print i
                continue
                
            title = _html2txt(i.split('</h3>')[0])
            try:
                    summary = _html2txt(re.findall(r'<div class="s">.*?<br>', i)[0]).replace('"','').replace("'","")
            except:
                    continue
            rev.append([url, title, summary])
    return rev

def getrev(key, engine='cn'):
    '''获取搜索结果
    return [[url, title, summary],]
    '''
    if engine=='cn':
        return _dorev(_gethtml_from_cn(key))
    elif engine=='com':
        return _dorev(_gethtml_from_com(key))
    else:
        return None
